package com.crm.dao.impl;

import com.crm.dao.IContactDao;
import com.crm.model.Contact;
import com.crm.model.User;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateCallback;
import org.hibernate.Session;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class ContactDao extends BaseDao implements IContactDao {

    /**
     * {@inheritDoc}
     */
    public void saveOrUpdate(Contact contact) {
        getHibernateTemplate().saveOrUpdate(contact);
    }

    /**
     * {@inheritDoc}
     */
    public List getContactAll() {
        return getHibernateTemplate().find("from Contact");
    }

    /**
     * {@inheritDoc}
     */
    public List getContactNameAll() {
        return getHibernateTemplate().find("select contactName from Contact");
    }

    /**
     * {@inheritDoc}
     */
    public List getContactsByUsercode(String userCode) {
        return getHibernateTemplate().find("from Contact where user.userCode=?", userCode);
    }

    /**
     * {@inheritDoc}
     * @param customerId
     */
    public List getContactByCustomerId(String customerId) {
        return getHibernateTemplate().find("from Contact where customer.id=?", customerId);
    }

    /**
     * {@inheritDoc}
     */
    public List getContactByCustomerId(String userCode, String customerId) {
        return getHibernateTemplate().find("from Contact where user.userCode=? and customer.id=?", new String[]{userCode, customerId});
    }

    /**
     * {@inheritDoc}
     */
    public Contact getContactById(String contactId) {
        List contactInfo = getHibernateTemplate().find("from Contact where id=?", contactId);
        if (contactInfo.isEmpty()) {
            return null;
        } else {
            return (Contact) contactInfo.get(0);
        }
    }

    /**
     * {@inheritDoc}
     */    
    public List getContactsByKeywords(String uGroup, String uCode, String customerName, String contactName, String email) {
        if (User.GROUP_SUPER.equals(uGroup) || User.GROUP_ADMIN.equals(uGroup)) {
        String[] arguments = new String[]{"%" + customerName + "%", "%" + contactName + "%", "%" + email + "%"};
        return getHibernateTemplate().find("from Contact where customer.customerName like ? and contactName like ? and email like ?", arguments);
        } else {
            String[] arguments = new String[]{uCode, "%" + customerName + "%", "%" + contactName + "%", "%" + email + "%"};
            return getHibernateTemplate().find("from Contact where user.userCode=? and customer.customerName like ? and contactName like ? and email like ?", arguments);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void delete(final Contact contact) {
        getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                return session.getNamedQuery("to_his_contacts").setString("id", contact.getId()).executeUpdate();
            }
        });
        getHibernateTemplate().delete(contact);
    }
    
    public int count(String customerId) {
        Long c = (Long) getHibernateTemplate().find("select count(id) from Contact where customer.id =?", customerId).get(0);
        return c.intValue();
    }

    public List getRecycleCont(final String customerId) {
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                return session.getNamedQuery("select_his_contacts")
                        .setString("customerId", customerId)
                        .list();
            }
        });
    }

}
